<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      ref="crud"
      v-model="form"
      v-model:page="page"
      :permission="permissionList"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template #menu="scope">
        <el-button
          type="primary"
          text
          size="default"
          icon="el-icon-view"
          @click.stop="handleViewInfo(scope.row)"
          >查看
        </el-button>
        <el-button
          type="primary"
          text
          size="default"
          icon="el-icon-user"
          @click.stop="handleViewUser(scope.row)"
          >查看用户
        </el-button>
      </template>
      <template #attachList="{ row }">
        <el-tag v-for="(tag, index) in row.attachList" :key="index">
          {{ tag.originalName }}
        </el-tag>
      </template>
    </avue-crud>
  </basic-container>

  <el-dialog title="查看" v-model="dialogVisible" width="800px">
    <div class="d_box">
      <div class="d_row">
        <p class="d_p">组织名称：</p>
        {{ rowData.orgName }}
      </div>
      <div class="d_row">
        <p class="d_p">机构代码</p>
        {{ rowData.orgCode }}
      </div>
      <div class="d_row">
        <p class="d_p">行政区域：</p>
        {{ rowData.adnm }}
      </div>
      <div class="d_row">
        <p class="d_p">申请人：</p>
        {{ rowData.creator }}
      </div>
      <div class="d_row">
        <p class="d_p">申请时间：</p>
        {{ rowData.createTime }}
      </div>
      <div class="d_row">
        <p class="d_p">申请材料：</p>
        <div class="d_cloum">
          <a :href="item.link" v-for="(item, index) in rowData.attachList" :key="item.id">{{
            item.originalName
          }}</a>
        </div>
      </div>
    </div>
  </el-dialog>

  <!-- 新增用户查看对话框 -->
  <el-dialog
    title="用户信息"
    v-model="userDialogVisible"
    width="600px"
    @closed="handleUserDialogClosed"
  >
    <user-table-with-pagination :org-id="currentOrgId" />
  </el-dialog>
</template>

<script>
import { list, userPageByOrg } from '@/api/organizational/organizationalManagement';
import { mapGetters } from 'vuex';
import UserTableWithPagination from './components/UserTableWithPagination.vue';

export default {
  components: {
    UserTableWithPagination,
  },
  data() {
    return {
      form: {},
      selectionList: [],
      query: { status: 2 },
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      option: {
        height: 'auto',
        indexLabel: '序号',
        indexWidth: '70',
        calcHeight: 32,
        tip: false,
        searchShow: true,
        searchMenuSpan: 3,
        border: true,
        index: true,
        selection: true,
        viewBtn: true,
        dialogClickModal: false,
        addBtn: false,
        header: false,
        delBtn: false,
        column: [
          {
            label: '组织名称',
            prop: 'orgName',
            search: true,
            slot: true,
          },
          {
            label: '机构代码',
            prop: 'orgCode',
            search: true,
          },
          {
            label: '行政区域',
            prop: 'adnm',
            disabled: true,
          },
          {
            label: '行政区域',
            prop: 'adcd',
            disabled: true,
            search: true,
            hide: true,
            display: false,
            type: 'cascader',
            checkStrictly: true,
            dicUrl: '/blade-system/region/lazy-tree',
            dicFormatter: res => {
              // 只返回江西省(id: 36)的数据
              return res.data.filter(item => item.id === '36');
            },
            dicQuery: {
              code: '36',
            },
            props: {
              label: 'title',
              value: 'id',
            },
          },
          {
            label: '创建人',
            prop: 'creator',
            disabled: true,
          },
          {
            label: '创建时间',
            prop: 'createTime',
            disabled: true,
          },
          {
            label: '上报材料',
            prop: 'attachList',
            hide: true,
            slot: true,
          },
        ],
      },
      data: [],
      dialogVisible: false,
      rowData: {},
      userDialogVisible: false,
      currentOrgId: null,
    };
  },
  computed: {
    ...mapGetters(['userInfo', 'permission']),
    permissionList() {
      return {
        addBtn: false,
        viewBtn: false,
        delBtn: true,
        editBtn: false,
      };
    },
  },
  methods: {
    handleViewInfo(row) {
      this.rowData = row;
      this.dialogVisible = true;
    },
    handleViewUser(row) {
      this.currentOrgId = row.id;
      this.userDialogVisible = true;
    },

    searchReset() {
      this.query = { status: 2 };
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = { status: 2 };
      this.page.currentPage = 1;
      if (params.adcd) {
        let arr = params.adcd[params.adcd.length - 1];
        params.adcd = arr;
      }
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },

    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      list(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    handleUserDialogClosed() {
      this.currentOrgId = '';
    },
  },
};
</script>
<style scoped lang="scss">
.d_box {
  padding: 0 20px;
  box-sizing: border-box;
  .d_row {
    display: flex;
    align-items: flex-start;
    margin: 18px 0;
    .d_p {
      width: 100px;
      margin: 0;
    }
    .d_cloum {
      display: block;
      flex-direction: column;
      a {
        display: block;
        margin-bottom: 8px;
        color: #2196f3;
      }
    }
  }
  .foot_box {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    margin: 20px 0;
  }
}
</style>
